package com.chuji.crmcj.adapter;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chuji.crmcj.R;
import com.chuji.crmcj.bean.ManageIndexPeterInfo;
import com.chuji.crmcj.utils.NumberUtils;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.utils.UrlUtils;
import com.jauker.widget.BadgeView;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by y.yiwen on 2/25/2016.
 */
public class CustomSalesPersonAdapter extends BaseAdapter{
    /*销售额和排名数据存储*/
    List<ManageIndexPeterInfo.ApiParamObjEntity> manageIndexPeterData;
    private Context context;
    //销售员列表
    private ListView mLv_salesperson;
    private ImageView iv_salesman_head;
    private TextView tv_salesman_name;
    private TextView tv_salesman_score;
    public CustomSalesPersonAdapter(Context context, List<ManageIndexPeterInfo.ApiParamObjEntity> manageIndexPeterData,ListView mLv_salesperson) {
        this.manageIndexPeterData = manageIndexPeterData;
        this.mLv_salesperson = mLv_salesperson;
    }
    @Override
    public int getCount() {
            return manageIndexPeterData.size();
    }

    @Override
    public Object getItem(int i) {
            return manageIndexPeterData.get(i);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        //判断convertView是否为null
            convertView =  LayoutInflater.from(UIUtils.getContext()).inflate(R.layout.listview_salesperson_mark, null);
            //初始化组件
            iv_salesman_head =(ImageView) convertView.findViewById(R.id.iv_salesman_head);
            tv_salesman_name = (TextView) convertView.findViewById(R.id.tv_salesman_name);
            tv_salesman_score = (TextView) convertView.findViewById(R.id.tv_salesman_score);
            //获得数据
            if(manageIndexPeterData.get(position).getAvatar()==null){
                iv_salesman_head.setImageResource(R.drawable.morentouxiang);
            }else {
//                iv_salesman_head.setImageResource(R.drawable.morentouxiang);
                Glide.with(UIUtils.getContext())
                        .load(UrlUtils.ImageIP+manageIndexPeterData.get(position).getAvatar())
                        .into(iv_salesman_head);
            }
            tv_salesman_name.setText(manageIndexPeterData.get(position).getRealName());
        if(manageIndexPeterData.get(position).getPerformance()==null){
            tv_salesman_score.setText("0");
        }else {
            BigDecimal bd = new BigDecimal(String.valueOf(manageIndexPeterData.get(position).getPerformance()));
            tv_salesman_score.setText(NumberUtils.parseMoney(",###,###.##", bd));
        }
        BadgeView badgeView = new BadgeView(UIUtils.getContext());
        switch (position){
            case 0:
                badgeView.setTargetView(iv_salesman_head);
                badgeView.setBadgeGravity(Gravity.RIGHT | Gravity.BOTTOM);
                badgeView.setBackgroundResource(R.drawable.paiming1);
//                badgeView.setBackground(10, Color.parseColor("#ff0000"));
                badgeView.setText("");
                break;
            case 1:
                badgeView.setTargetView(iv_salesman_head);
                badgeView.setBadgeGravity(Gravity.RIGHT | Gravity.BOTTOM);
                badgeView.setBackgroundResource(R.drawable.paiming2);
//                badgeView.setBackground(10, Color.parseColor("#ff0000"));
                badgeView.setText("");
                break;
            case 2:
                badgeView.setTargetView(iv_salesman_head);
                badgeView.setBadgeGravity(Gravity.RIGHT | Gravity.BOTTOM);
                badgeView.setBackgroundResource(R.drawable.paiming3);
//                badgeView.setBackground(10, Color.parseColor("#ff0000"));
                badgeView.setText("");
                break;
        }
        return convertView;
    }
}
